package com.sky.service.impl;

import com.sky.dto.GoodsSalesDTO;
import com.sky.entity.Orders;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.reportService;
import com.sky.vo.OrderReportVO;
import com.sky.vo.SalesTop10ReportVO;
import com.sky.vo.TurnoverReportVO;
import com.sky.vo.UserReportVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@Service
public class reportServiceImpl implements reportService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private UserMapper userMapper;
    @Override
    public TurnoverReportVO getTurnover(LocalDate begin, LocalDate end) {
        List<LocalDate> dateList = new ArrayList<>();
        for (LocalDate date = begin; date.isBefore(end); date = date.plusDays(1)) {
            dateList.add(date);
        }
        List<Double> turnoverList = new ArrayList<>();
        for (LocalDate date : dateList) {
            LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);
            // 如果需要手动格式化为字符串
            Map map = new HashMap();
            map.put("status", Orders.PENDING_PAYMENT);
            map.put("begin", beginTime);
            map.put("end", endTime);
            Double turnover = orderMapper.sumByMap(map);
            turnover = turnover == null ? 0.0 : turnover;
            turnoverList.add(turnover);
            // turnoverList.add(orderMapper.sumByMap(map));
        }
        TurnoverReportVO turnoverReportVO = TurnoverReportVO.builder()
                .dateList(StringUtils.join(dateList, ","))
                .turnoverList(StringUtils.join(turnoverList, ","))
                .build();
        return turnoverReportVO;
    }

    @Override
    public UserReportVO getUser(LocalDate begin, LocalDate end) {
        List<LocalDate> dateList2 = new ArrayList<>();
        for (LocalDate date = begin; date.isBefore(end); date = date.plusDays(1)) {
            dateList2.add(date);
        }
        List<Integer> allUserList = new ArrayList<>();
        List<Integer> IncrementUserList = new ArrayList<>();
        for (LocalDate date : dateList2) {
            LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);
            // 如果需要手动格式化为字符串
            Map map = new HashMap();
            // map.put("status", Orders.PENDING_PAYMENT);
            map.put("begin", beginTime);
            map.put("end", endTime);
            //  查表里所有的用户总数
            Integer totalUser = userMapper.sumByMap(map);
            Map map1 = new HashMap();
            map1.put("begin", null);
            map1.put("end", endTime);
            Integer newUser = userMapper.sumByMap(map);
            allUserList.add(totalUser);
            IncrementUserList.add(newUser);
        }
        UserReportVO userReportVO = UserReportVO.builder()
                .dateList(StringUtils.join(dateList2, ","))
                .totalUserList(StringUtils.join(allUserList, ","))
                .newUserList(StringUtils.join(IncrementUserList, ","))
                .build();
        return userReportVO;
        //  return null;
    }
    // 订单模块
    @Override
    public OrderReportVO getOrderStatistics(LocalDate begin, LocalDate end) {
        List<LocalDate> dateList3 = new ArrayList<>();
        for (LocalDate date = begin; date.isBefore(end); date = date.plusDays(1)) {
            dateList3.add(date);
        }
        List<Integer> allUserList = new ArrayList<>();
        List<Integer> IncrementUserList = new ArrayList<>();
        for (LocalDate date : dateList3) {
            LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);
            // 如果需要手动格式化为字符串
            Map map = new HashMap();
            // map.put("status", Orders.PENDING_PAYMENT);
            map.put("begin", beginTime);
            map.put("end", endTime);
            //  每天的订单
            Integer orderDay = orderMapper.sumOrderCount(map);
            Map map1 = new HashMap();
            map1.put("begin", beginTime);
            map1.put("end", endTime);
            map1.put("status", Orders.COMPLETED);
           Integer orderCompleted = orderMapper.sumOrderCount(map);
            allUserList.add(orderDay);
            IncrementUserList.add(orderCompleted);
        }
        OrderReportVO orderReportVO = OrderReportVO.builder()
                .dateList(StringUtils.join(dateList3, ","))
                .orderCountList(StringUtils.join(allUserList, ","))
                .validOrderCountList(StringUtils.join(IncrementUserList, ","))
                .build();
        return orderReportVO;
    }

    @Override
    public SalesTop10ReportVO  getTop10(LocalDate begin, LocalDate end) {
        List<LocalDate> dateList = new ArrayList<>();
        LocalDateTime beginTime = LocalDateTime.of(begin, LocalTime.MIN);
        LocalDateTime endTime = LocalDateTime.of(end, LocalTime.MAX);
        for (LocalDate date = begin; date.isBefore(end); date = date.plusDays(1)) {
            dateList.add(date);
        }
        // Map map = new HashMap();
        Map<String, Object> map = new HashMap();
        map.put("begin",beginTime);
        map.put("end",endTime);
        map.put("status",Orders.COMPLETED);
        // 不要把整张表都返回给前端，这样会浪费数据库的性能，前端用到的列返回即可
        List<GoodsSalesDTO> salesTop10List = orderMapper.getSalesTop10(map);
        // 先转为字符串，再将字符串再转为数组
        String nameList = StringUtils.join(salesTop10List.stream().map(GoodsSalesDTO::getName).collect(Collectors.toList()), ",");
        String numberList = StringUtils.join(salesTop10List.stream().map(GoodsSalesDTO::getNumber).collect(Collectors.toList()), ",");
        SalesTop10ReportVO salesTop10ReportVO = new SalesTop10ReportVO();
        salesTop10ReportVO.setNameList(nameList);
        salesTop10ReportVO.setNumberList(numberList);
        return salesTop10ReportVO;
    }
}
